﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

using RTGS.Library;
using RTGS.Library.Entities.Rtgs;
using RTGS.Library.Enum;
using RTGS.Library.Interfaces;

namespace RTGS.Library.DataAccess.Rtgs
{
    public partial class stpRecordDetailORepo
    {
        public bool UpdateStatus(stpRecordDetailO domain)
        {
            int result = 0;
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    StringBuilder sql = new StringBuilder();

                    sql.AppendLine("UPDATE STP_RECORD_DETAIL_O SET RECORD_DETAIL_ORECORD_Status = @recordDetailOrecordStatus");
                    sql.AppendLine(" WHERE RECORD_DETAIL_OTR_RelatedTRN = @recordDetailOtrRelatedtrn");

                    cmd.CommandText = sql.ToString();

                    cmd.Parameters.Add("@recordDetailOrecordStatus", SqlDbType.Char).Value = (object)domain.recordDetailOrecordStatus ?? DBNull.Value;
                    cmd.Parameters.Add("@recordDetailOtrRelatedtrn", SqlDbType.VarChar).Value = domain.recordDetailOtrRelatedtrn;


                    try
                    {
                        conn.Open();
                        result = cmd.ExecuteNonQuery();
                    }
                    catch (Exception ex)
                    {
                        errorLog = ex.Message;
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return (result > 0);
        }

        public bool SaveFromDetailI(stpRecordDetailO domain)
        {
            bool result = false;
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = @"INSERT INTO STP_RECORD_DETAIL_O(
 RECORD_DETAIL_OTR_TransactionCode
, RECORD_DETAIL_OTR_FromMember
, RECORD_DETAIL_OTR_ToMember
, RECORD_DETAIL_OTR_TRN
, RECORD_DETAIL_OTR_RelatedTRN
, RECORD_DETAIL_OTR_Amount
, RECORD_DETAIL_OTR_ValueDate
, RECORD_DETAIL_OST_ToAccountNumber
, RECORD_DETAIL_OST_ToAccountName
, RECORD_DETAIL_OST_FromAccountNumber
, RECORD_DETAIL_OST_FromAccountName
, RECORD_DETAIL_OST_MemberInfo
, RECORD_DETAIL_OST_OriginatingNumber
, RECORD_DETAIL_OST_OriginatingName
, RECORD_DETAIL_OST_UltimateBenefAccount
, RECORD_DETAIL_OST_UltimateBenefName
, RECORD_DETAIL_ORECORD_Status
, RECORD_DETAIL_ORECORD_Rsncde
,RECORD_DETAIL_ORecordTime
,RECORD_DETAIL_OTR_RecordType
,RECORD_DETAIL_OST_PaymentDetails
) 
VALUES(
  @recordDetailOtrTransactioncode
, @recordDetailOtrFrommember
, @recordDetailOtrTomember
, @recordDetailOtrTrn
, @recordDetailOtrRelatedtrn
, @recordDetailOtrAmount
, @recordDetailOtrValuedate
, @recordDetailOstToaccountnumber
, @recordDetailOstToaccountname
, @recordDetailOstFromaccountnumber
, @recordDetailOstFromaccountname
, @recordDetailOstMemberinfo
, @recordDetailOstOriginatingnumber
, @recordDetailOstOriginatingname
, @recordDetailOstUltimatebenefaccount
, @recordDetailOstUltimatebenefname
, @recordDetailOrecordStatus
, @recordDetailOrecordRsncde
, @recordDetailOrecordtime
, @recordDetailOtrRecordtype
,@recordDetailOstPaymentdetails
)";
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.Add("@recordDetailOtrTransactioncode", SqlDbType.VarChar).Value = (object)domain.recordDetailOtrTransactioncode ?? DBNull.Value;
                    cmd.Parameters.Add("@recordDetailOtrFrommember", SqlDbType.VarChar).Value = (object)domain.recordDetailOtrFrommember ?? DBNull.Value;
                    cmd.Parameters.Add("@recordDetailOtrTomember", SqlDbType.VarChar).Value = (object)domain.recordDetailOtrTomember ?? DBNull.Value;
                    cmd.Parameters.Add("@recordDetailOtrTrn", SqlDbType.VarChar).Value = (object)domain.recordDetailOtrTrn ?? DBNull.Value;
                    cmd.Parameters.Add("@recordDetailOtrRelatedtrn", SqlDbType.VarChar).Value = domain.recordDetailOtrRelatedtrn;
                    cmd.Parameters.Add("@recordDetailOtrAmount", SqlDbType.VarChar).Value = (object)domain.recordDetailOtrAmount ?? DBNull.Value;
                    cmd.Parameters.Add("@recordDetailOtrValuedate", SqlDbType.VarChar).Value = (object)domain.recordDetailOtrValuedate ?? DBNull.Value;
                    cmd.Parameters.Add("@recordDetailOstToaccountnumber", SqlDbType.VarChar).Value = (object)domain.recordDetailOstToaccountnumber ?? DBNull.Value;
                    cmd.Parameters.Add("@recordDetailOstToaccountname", SqlDbType.VarChar).Value = (object)domain.recordDetailOstToaccountname ?? DBNull.Value;
                    cmd.Parameters.Add("@recordDetailOstFromaccountnumber", SqlDbType.VarChar).Value = (object)domain.recordDetailOstFromaccountnumber ?? DBNull.Value;
                    cmd.Parameters.Add("@recordDetailOstFromaccountname", SqlDbType.VarChar).Value = (object)domain.recordDetailOstFromaccountname ?? DBNull.Value;
                    cmd.Parameters.Add("@recordDetailOstMemberinfo", SqlDbType.VarChar).Value = (object)domain.recordDetailOstMemberinfo ?? DBNull.Value;
                    cmd.Parameters.Add("@recordDetailOstOriginatingnumber", SqlDbType.VarChar).Value = (object)domain.recordDetailOstOriginatingnumber ?? DBNull.Value;
                    cmd.Parameters.Add("@recordDetailOstOriginatingname", SqlDbType.VarChar).Value = (object)domain.recordDetailOstOriginatingname ?? DBNull.Value;
                    cmd.Parameters.Add("@recordDetailOstUltimatebenefaccount", SqlDbType.VarChar).Value = (object)domain.recordDetailOstUltimatebenefaccount ?? DBNull.Value;
                    cmd.Parameters.Add("@recordDetailOstUltimatebenefname", SqlDbType.VarChar).Value = (object)domain.recordDetailOstUltimatebenefname ?? DBNull.Value;
                    cmd.Parameters.Add("@recordDetailOrecordStatus", SqlDbType.Char).Value = (object)domain.recordDetailOrecordStatus ?? DBNull.Value;
                    cmd.Parameters.Add("@recordDetailOrecordRsncde", SqlDbType.Char).Value = (object)domain.recordDetailOrecordRsncde ?? DBNull.Value;
                    cmd.Parameters.Add("@recordDetailOrecordtime", SqlDbType.DateTime).Value = domain.recordDetailOrecordtime;
                    cmd.Parameters.Add("@recordDetailOtrRecordtype", SqlDbType.Char).Value = (object)domain.recordDetailOtrRecordtype ?? DBNull.Value;
                    cmd.Parameters.Add("@recordDetailOstPaymentdetails", SqlDbType.VarChar).Value = (object)domain.recordDetailOstPaymentdetails ?? DBNull.Value;

                    try
                    {
                        conn.Open();
                        cmd.ExecuteNonQuery();
                        result = true;
                    }
                    catch (Exception ex)
                    {
                        errorLog = ex.Message;
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }

                }
            }
            return result;
        }

        public bool UpdateStatusCancel(stpRecordDetailO domain)
        {
            int result = 0;
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    StringBuilder sql = new StringBuilder();

                    sql.AppendLine("UPDATE STP_RECORD_DETAIL_O SET RECORD_DETAIL_ORECORD_Status = @recordDetailOrecordStatus,RECORD_DETAIL_ORECORD_Rsncde=@recordDetailOrecordRsncde");
                    sql.AppendLine(" WHERE RECORD_DETAIL_OTR_RelatedTRN = @recordDetailOtrRelatedtrn");

                    cmd.CommandText = sql.ToString();

                    cmd.Parameters.Add("@recordDetailOrecordStatus", SqlDbType.Char).Value = (object)domain.recordDetailOrecordStatus ?? DBNull.Value;
                    cmd.Parameters.Add("@recordDetailOtrRelatedtrn", SqlDbType.VarChar).Value = domain.recordDetailOtrRelatedtrn;
                    cmd.Parameters.Add("@recordDetailOrecordRsncde", SqlDbType.VarChar).Value = domain.recordDetailOrecordRsncde;


                    try
                    {
                        conn.Open();
                        result = cmd.ExecuteNonQuery();
                    }
                    catch (Exception ex)
                    {
                        errorLog = ex.Message;
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return (result > 0);
        }
    }
}
